package com.yss.kettle.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.kettle.domain.JobTask;
import com.yss.kettle.domain.JobTaskDetails;
import com.yss.kettle.service.IJobTaskDetailsService;
import com.yss.kettle.service.IJobTaskService;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.kettle.service.IJobRecordService;
import com.yss.kettle.mapper.JobRecordMapper;
import com.yss.kettle.domain.JobRecord;
import org.springframework.util.CollectionUtils;

/**
 * 日志信息Service业务层处理
 *
 * @author yss
 * @date 2023-04-25
 */
@Slf4j
@Service
public class JobRecordServiceImpl extends ServiceImpl<JobRecordMapper, JobRecord> implements IJobRecordService {
    @Autowired
    private IJobTaskService jobTaskService;
    @Autowired
    private IJobTaskDetailsService jobTaskDetailsService;

    /**
     * 查询日志信息列表
     *
     * @param jobRecord 日志信息
     * @return 日志信息
     */
    @Override
    public List<JobRecord> selectJobRecordList(JobRecord jobRecord) {
        QueryWrapper<JobRecord> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(jobRecord.getParams())) {
            if (null != jobRecord.getParams().get("beginTime") && StringUtils.isNotEmpty(jobRecord.getParams().get("beginTime").toString())) {
                wrapper.ge("START_TIME", jobRecord.getParams().get("beginTime"));
            }
            if (null != jobRecord.getParams().get("endTime") && StringUtils.isNotEmpty(jobRecord.getParams().get("endTime").toString())) {
                wrapper.le("START_TIME", jobRecord.getParams().get("endTime"));
            }
        }
        wrapper.orderByDesc("START_TIME");
        List<JobRecord> list = this.list(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        List<JobTask> jobTaskList = jobTaskService.list();
        List<JobTaskDetails> jobTaskDetailsList = jobTaskDetailsService.list();
        Map<String, String> map = new HashMap<>();
        if (!CollectionUtils.isEmpty(jobTaskList)) {
            for (JobTask jobTask : jobTaskList) {
                if (null == jobTask) {
                    continue;
                }
                map.put(jobTask.getId(), jobTask.getName());
            }
        }
        Map<String, String> mapTaskDetails = new HashMap<>();
        if (!CollectionUtils.isEmpty(jobTaskDetailsList)) {
            for (JobTaskDetails details : jobTaskDetailsList) {
                if (null == details) {
                    continue;
                }
                mapTaskDetails.put(details.getId(), details.getTaskId());
            }
        }
        for (JobRecord record : list) {
            if (null == record) {
                continue;
            }
            if (1 == record.getFtype() && map.containsKey(record.getJobId())) {
                record.setJobName(map.get(record.getJobId()));
            }
            if (2 == record.getFtype() && mapTaskDetails.containsKey(record.getJobId())
                    && map.containsKey(mapTaskDetails.get(record.getJobId()))) {
                record.setJobName(map.get(mapTaskDetails.get(record.getJobId())));
            }
        }
        return list;
    }

}
